<?php
namespace app\admin\controller;

use app\admin\controller\Base;
use app\admin\model\Admin;
use app\admin\model\Menu;
use app\admin\model\Setting;
use think\Request;

class  Menus extends Base
{
    //列表
    public function lists(){
        $user = $this->user_info();
        $menu = Menu::select();
        
        foreach($menu as $k => $val){
            if($val['pid'] == 0){
                $menu[$k]['pname'] = "顶级栏目";
            }else {
                $res = Menu::where(array("id" => $val['pid']))->find();
                $menu[$k]['pname'] = $res['title'];
            }
        }
        //修改所属栏目
        $data = array(
            "setting" => $this->getAllSetting(),
            "list" =>$menu,
        );
        return view("list",$data);
    }

    //添加
    public function addform(){
        $user = $this->user_info();
        $data = array(
            "setting" => $this->getAllSetting(),
            "list"=>Menu::where(array("pid"=>"0"))->order("sort asc")->select(),
        );
        return view("addform",$data);
    }
    public function add(Request $request){
        $data = $request->post();
        if(Menu::insert($data)){
            $this->success("添加成功","Menus/lists");
        }else{
            $this->error("添加失败","Menus/addform");
        }

    }
    //编辑
    public function update(Request $request){
        $user = $this->user_info();
        $menu = Menu::where("pid","0")->order("sort asc")->select();
        foreach($menu as $k => $val){
            if($val['pid'] == 0){
                $menu[$k]['pname'] = "顶级栏目";
            }else {
                $res = Menu::where(array("id" => $val['pid']))->find();
                $menu[$k]['pname'] = $res['title'];
            }
        }
        $data = array(
            "setting" => $this->getAllSetting(),
            "data" => Menu::where(array('id'=>$request->get('id')))->find(),
            "list" => $menu,
        );
        return view("update",$data);
    }
    public function edit(Request $request){
        $data = $request->post();
        if(Menu::update($data)){
            $this->success("修改成功","Menus/lists");
        }else{
            $this->error("修改失败","Menus/update");
        }

    }
    //删除
    public function delete(Request $request){
        $user = $this->user_info();
        if(Menu::destroy($request->post('id'))){
            $this->success("删除成功","Menus/lists");
        }else{
            $this->error("删除失败","Menus/lists");
        }
    }

    //所有图标页面
    public function fontawesome(){
        $user = $this->user_info();
        $data = array(
            "setting" => $this->getAllSetting(),
        );
        return view("fontawesome",$data);
    }

    //修改状态
    public function setStatus(Request $request){
        $data = $request->post();
        if(Menu::update($data)){
            $this->success("成功");
        }else{
            $this->error("失败");
        }
    }

}
